/**
 * 
 */
package com.ygj.ticketManage.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.transaction.annotation.Transactional;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.dao.EmployeeCompoundsMapper;
import com.base.common.persistent.dao.EmployeeMapper;
import com.base.common.persistent.dao.TicketMapper;
import com.base.common.persistent.dao.TicketReportMapper;
import com.base.common.persistent.model.Compounds;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.Subclass;
import com.base.common.persistent.model.Ticket;
import com.cjy.common.bean.OpAttributes;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/**
 * @author K
 *
 */

@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class HelloAction extends AbstractBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6264415614442309803L;
	
	
	@Resource
	private TicketMapper ticketMapper;

	@Resource
	private TicketReportMapper ticketReportMapper;

	@Resource
	private EmployeeMapper employeeMapper;
	
	@Resource
	private EmployeeCompoundsMapper employeeCompoundsMapper;

	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosFileName;
	private String[] filePhotosContentType;

	public File[] getFilePhotos() {
		return filePhotos;
	}

	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}

	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}

	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}

	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}

	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	public TicketMapper getTicketMapper() {
		return ticketMapper;
	}

	public void setTicketMapper(TicketMapper ticketMapper) {
		this.ticketMapper = ticketMapper;
	}

	public TicketReportMapper getTicketReportMapper() {
		return ticketReportMapper;
	}

	public void setTicketReportMapper(TicketReportMapper ticketReportMapper) {
		this.ticketReportMapper = ticketReportMapper;
	}

	public EmployeeMapper getEmployeeMapper() {
		return employeeMapper;
	}

	public void setEmployeeMapper(EmployeeMapper employeeMapper) {
		this.employeeMapper = employeeMapper;
	}

	public EmployeeCompoundsMapper getEmployeeCompoundsMapper() {
		return employeeCompoundsMapper;
	}

	public void setEmployeeCompoundsMapper(EmployeeCompoundsMapper employeeCompoundsMapper) {
		this.employeeCompoundsMapper = employeeCompoundsMapper;
	}

	/**
	 * 提交投诉信息
	 * 
	 * @return
	 */
	@Transactional
	@Action(value = "subHello", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String subTest() {

		try {
			for (int j = 0; j < filePhotos.length; j++) {
				Workbook workbook = Workbook.getWorkbook(filePhotos[j]);
				Sheet[] sheet = workbook.getSheets();
				for (int i = 0; i < sheet.length; i++) {
					int rows = sheet[i].getRows();
					for (int k = 2; k < rows; k++) {
						Cell[] cells = sheet[i].getRow(k);
						if (cells[2].getContents() != null && !"".equals(cells[2].getContents().trim())) {
							System.out.println( k + "行");
							Ticket ticket = new Ticket();
							ticket.setContext(cells[6].getContents());
							ticket.setPicUrls(null);
							ticket.setStatus("完成".equals(cells[4].getContents()) ? 4 : 5);
							ticket.setSubUserPhone(cells[5].getContents());
							// 设置员工小区中间表id
							if(cells[9].getContents() != null && !"".equals(cells[9].getContents().trim())){
								
								List<Employee> employees = employeeMapper
										.queryEmployeesByCompoundsIdAndNameAll(31, cells[9].getContents());
								if(employees.size() > 0){
									int employeeCompoundsId = employeeCompoundsMapper.queryEmployeeCompoundsIdByEmployeeIdAndCompoundsId(employees.get(0).getId(), 31);
									ticket.setEmployeeCompoundsId(employeeCompoundsId);
									ticket.setEmployee(new Employee().setId(employees.get(0).getId()));
									ticket.setCompounds(new Compounds().setId(31));
								}
							}
							
							ticket.setCategories("随手拍工单".equals(cells[2].getContents().trim()) ? 5 : ("调度中心工单".equals(cells[2].getContents().trim()) ? 6 : -1));
							// System.out.println(cells[0].getContents().trim());
							/* Calendar calendar = new GregorianCalendar();
							 calendar.set(1900, 0,
							 Integer.parseInt(cells[0].getContents().trim()) -
							 1, 0, 0, 0);*/
							// Date date = new
							// Date(Integer.parseInt(cells[0].getContents().trim()));
							SimpleDateFormat format1 = new SimpleDateFormat("MM/d/yy H:mm");

							SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
							// System.out.println(format.format(calendar.getTime())
							// + "\t" + cells[4].getContents());

							if(cells[7].getContents() != null && !"".equals(cells[7].getContents().trim())){
								ticket.setGetTime(format.format(format1.parse(cells[7].getContents())));
							}
							
							if(cells[8].getContents() != null && !"".equals(cells[8].getContents().trim())){
								ticket.setFinishTime(format.format(format1.parse(cells[8].getContents())));
							}
							System.out.println(cells[7].getContents() + ticket.getGetTime());
							String code = this.getComlaintCode(ticket.getCategories() == 6 ? "G" : "F",
									ticket.getCategories());
							ticket.setCode(code);
							
							Subclass subclass = ticketMapper.queryMaintenanceClassByName(cells[3].getContents().trim());
							if(subclass != null){
								ticket.setSubclass(subclass);
							}
							System.out.println(ticketMapper.addTicket(ticket));
							
						}

					}
				}
			}

			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1));

		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}

		return returnType();
	}

	/**
	 * 生成调度中心工单号
	 * 
	 * @param tickets
	 * @return
	 */
	private String getComlaintCode(String prefixCode, int categories) {
		List<Ticket> tickets = new ArrayList<Ticket>();
		try {
			tickets = ticketMapper.queryLastCodeByCategories(categories);
		} catch (Exception e) {
			logger.info(e.fillInStackTrace());
			return null;
		}
		String code = "";
		DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
		if (tickets.size() != 0) {
			String lastedCode = tickets.get(0).getCode();
			System.out.println(lastedCode);
			code = df.format(Integer.parseInt(lastedCode.substring(1).trim()) + 1);
			System.out.println(code);
		} else {
			code = df.format(1);
		}
		code = prefixCode + code;
		return code;
	}

}
